<template>
  <view class="task-card" :class="{ 'completed': task.completed }">
    <view class="task-header">
      <view class="task-icon">
        <u-icon :name="task.icon || 'star-fill'" :color="iconColor" size="40" />
      </view>
      <view class="task-info">
        <text class="task-name">{{ task.name }}</text>
        <text class="task-description">{{ task.description }}</text>
      </view>
      <view class="task-points">
        <text class="points-value">+{{ task.points }}</text>
        <text class="points-unit">积分</text>
      </view>
    </view>
    
    <view class="task-progress" v-if="task.progress !== undefined">
      <view class="progress-bar">
        <view 
          class="progress-fill" 
          :style="{ width: `${Math.min(task.progress / task.maxProgress * 100, 100)}%` }"
        />
      </view>
      <text class="progress-text">{{ task.progress }}/{{ task.maxProgress }}</text>
    </view>
    
    <view class="task-footer">
      <view class="task-tags">
        <text 
          v-for="tag in task.tags" 
          :key="tag" 
          class="task-tag"
        >
          {{ tag }}
        </text>
      </view>
      <view class="task-actions">
        <custom-button
          v-if="!task.completed"
          :text="task.buttonText || '去完成'"
          :type="task.custom ? 'secondary' : 'primary'"
          size="small"
          :loading="loading"
          @click="handleAction"
        />
        <view v-else class="completed-badge">
          <u-icon name="checkmark" color="#34c759" size="24" />
          <text>已完成</text>
        </view>
        
        <view 
          v-if="task.custom" 
          class="delete-btn" 
          @click="handleDelete"
        >
          <u-icon name="trash" color="#ff4757" size="28" />
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import CustomButton from '@/components/custom-button/index.vue'

export default {
  name: 'TaskCard',
  components: {
    CustomButton
  },
  props: {
    task: {
      type: Object,
      required: true
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    iconColor() {
      if (this.task.completed) return '#34c759'
      if (this.task.custom) return '#ff9500'
      return '#007aff'
    }
  },
  methods: {
    handleAction() {
      this.$emit('action', this.task)
    },
    handleDelete() {
      this.$emit('delete', this.task)
    }
  }
}
</script>

<style lang="scss" scoped>
.task-card {
  background: #fff;
  border-radius: 16rpx;
  padding: 32rpx;
  margin-bottom: 24rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
  
  &.completed {
    opacity: 0.7;
    background: #f8f8f8;
}
}

.task-header {
  display: flex;
  align-items: flex-start;
  margin-bottom: 24rpx;
}

.task-icon {
  margin-right: 24rpx;
  width: 80rpx;
  height: 80rpx;
  background: rgba(0, 122, 255, 0.1);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.task-info {
  flex: 1;
  margin-right: 24rpx;
}

.task-name {
  font-size: 32rpx;
  font-weight: 500;
  color: #333;
  display: block;
  margin-bottom: 8rpx;
}

.task-description {
  font-size: 26rpx;
  color: #666;
  line-height: 1.4;
}

.task-points {
  text-align: right;
  
  .points-value {
    font-size: 36rpx;
    font-weight: bold;
    color: #ff4757;
    display: block;
}
  
  .points-unit {
    font-size: 24rpx;
    color: #999;
}
}

.task-progress {
  margin-bottom: 24rpx;
  display: flex;
  align-items: center;
   margin-right: 16rpx; &:last-child { margin-right: 0; }
}

.progress-bar {
  flex: 1;
  height: 8rpx;
  background: #f0f0f0;
  border-radius: 4rpx;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #007aff, #5856d6);
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 24rpx;
  color: #666;
  min-width: 80rpx;
  text-align: right;
}

.task-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.task-tags {
  display: flex;
   margin-right: 12rpx; &:last-child { margin-right: 0; }
  flex-wrap: wrap;
}

.task-tag {
  font-size: 22rpx;
  color: #666;
  background: #f5f5f5;
  padding: 4rpx 12rpx;
  border-radius: 12rpx;
}

.task-actions {
  display: flex;
  align-items: center;
   margin-right: 16rpx; &:last-child { margin-right: 0; }
}

.completed-badge {
  display: flex;
  align-items: center;
   margin-right: 8rpx; &:last-child { margin-right: 0; }
  font-size: 24rpx;
  color: #34c759;
}

.delete-btn {
  padding: 8rpx;
  cursor: pointer;
}
</style>